package com.hl.card.modular.packBack.controller;

import com.alibaba.fastjson.JSONObject;
import com.hl.card.common.persistence.model.Enrolment;
import com.hl.card.common.remote.user.RemoteUser;
import com.hl.card.common.remote.user.RemoteUserService;
import com.hl.card.common.remote.util.CommonUtilService;
import com.hl.card.common.util.HttpRequestUtil;
import com.hl.card.modular.pack.service.RemoteInterface;

import com.hl.card.modular.packBack.service.SuperPartnerActiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("api/test")
public class TestController {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private RemoteInterface remoteInterface;

    @Autowired
    private RemoteUserService remoteUser;

    @Autowired
    private CommonUtilService commonUtilService;


    @Autowired
    SuperPartnerActiveService superPartnerActiveService;

    @RequestMapping("")
    public String index(){
        return "/test.html";

    }

    public String hello() {
        String gustId = "aaa";
        String createText = "bbb";
        redisTemplate.opsForValue().set(gustId, createText, 60 * 60 * 60, TimeUnit.SECONDS);
        String getCode = redisTemplate.opsForValue().get(gustId);
        System.out.println(getCode);
        return "hello";
    }

    @RequestMapping("/remote")
    @ResponseBody
    public Object testRemote(String phone) {
        return commonUtilService.getPhoneCode(phone);
    }

    @RequestMapping("/sendpCode")
    @ResponseBody
    public Object sendpCode(String phone) {
        JSONObject jsonObject=remoteUser.sendPCode("13526651696","123456");
        if(jsonObject.get("code").equals("0")){
            System.out.println("发送成功");
        }else{
            System.out.println("发送失败");
        }
        return null;
    }

    @RequestMapping(value = "/a", method = RequestMethod.GET)
    public String getCode(String a){
       return  remoteUser.sayHiFromClientOne(a);
    }

    @RequestMapping(value = "/getuser", method = RequestMethod.GET)
    @ResponseBody
    public RemoteUser getUser(String phone){
        RemoteUser user=null;
        JSONObject object=remoteUser.findSsoUserByPhone(phone);
        if(object.get("code").equals("0")){
            user= object.getObject("user",RemoteUser.class);
        }
        return  user;
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public Map<String,Object>   upload(@RequestParam("file") MultipartFile file){
        List<Enrolment> enrolments=new ArrayList<>();
        Map<String,Object> res = new HashMap<>();
        //上传错误信息
        String erroMsg="";
        Map<String, Object> result=superPartnerActiveService.transFile(file);
        if(result.get("code").equals("1")){
            enrolments = (List<Enrolment>) result.get("enrolments");

//            Map<String,Object> map = superPartnerActiveService.insertBatch(enrolments);
            res.put("code",200);
            res.put("msg","插入成功");
//            res.put("data",map);
        }else{
            erroMsg= (String) result.get("msg");
            res.put("code",201);
            res.put("msg",erroMsg);
        }
        return res;
    }

    @RequestMapping("test1")
    @ResponseBody
    public String test1(){
        String test1="你好呀老大";
        String param="a="+test1;
        HttpRequestUtil.sendPost("http://10.2.4.44:6062/api/test/test2",param,false);
        return test1;
   }

    @RequestMapping("test2")
    @ResponseBody
    public String test1(String a){
        System.out.println(a);
        return a;
    }


}
